<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/18  11:58
// +----------------------------------------------------------------------
namespace Common\Model;
use Think\Model;

/*
 * User模型
 * 主要针对用户的常用信息获取，如是否会员，用户的各个可免次数等信息
 * */
class UserModel extends Model
{
    protected $tableName="User";
    //用户是否是会员,id不填默认当前用户
    function is_vip($user_id=""){
        if($user_id==""){
            $user_id=session("user_id");
        }
        $Card=M("Card");
        $map["user_id"]=$user_id;
        $dc=$Card->where("user_id=$user_id")->find();
        if(strtotime($dc['create_time']) < time() && strtotime($dc['expire_time']) > time())
        {
            return 1;
        }else{
            return 0;
        }
    }
    //获取当前用户 活动报名费可免次数
    function getFreenum_apply(){
        $user_id=session("user.user_id");
        $Card=M("Card");

        if(!$this->is_vip()){
            return 0;
        }
        $d=$Card->where("user_id=$user_id")->field("num_activity")->find();
        return $d["num_activity"];
    }
    //获取当前用户 活动报名费折扣
    function getDiscount_apply(){

        $user_id=session("user.user_id");

        $Card=M("Card");
        if(!$this->is_vip()){
            return 1;
        }
        $d=$Card->where("user_id=$user_id")->field("discount_apply")->find();
        return $d["discount_apply"];
    }
    //获取用户的借阅运费
    //根据所在地区获取
    function getFreightRent($user_id=""){
        if($user_id==""){
            $user_id=session("user.user_id");
        }
        $adr=M("Address");
        $dadd=$adr->where("user_id=$user_id AND is_default=1")->field("province,city,district")->find();

        $dis=M("District");
        $map["name"]=$dadd['province'];
        $map["level"]=1;
        $dp=$dis->where($map)->field("id")->find();

        $map["name"]=$dadd['city'];
        $map["level"]=2;
        $map["pid"]=$dp['id'];
        $dc=$dis->where($map)->field("id")->find();

        $map["name"]=$dadd['district'];
        $map["level"]=3;
        $map["pid"]=$dc['id'];
        $dd=$dis->where($map)->field("price_rent")->find();
        if($dd){
            return $dd["price_rent"];
        }else{
            return false;
        }
    }
    //获取用户的购书运费
    //根据所在地区获取
    function getFreightBuy($user_id=""){
        if($user_id==""){
            $user_id=session("user_id");
        }
        $adr=M("Address");
        $dadd=$adr->where(["user_id"=>$user_id,"is_default"=>1])->field("province,city,district")->find();

        $dis=M("District");
        $map["name"]=$dadd['province'];
        $map["level"]=1;
        $dp=$dis->where($map)->field("id")->find();

        $map["name"]=$dadd['city'];
        $map["level"]=2;
        $map["pid"]=$dp['id'];
        $dc=$dis->where($map)->field("id")->find();

        $map["name"]=$dadd['district'];
        $map["level"]=3;
        $map["pid"]=$dc['id'];
        $dd=$dis->where($map)->field("price_buy")->find();
        if($dd){
            return $dd["price_buy"];
        }else{
            return false;
        }
    }
    //获取用户的地址
    function getAddress($user_id=""){
        if($user_id==""){
            $user_id=session("user_id");
        }
        $Adr=M("Address");
        $dadr=$Adr->where(["user_id"=>$user_id,"is_default"=>1])->field("name,phone,province,city,district,address")->find();
        $d=array(
            "name"=>$dadr['name'],
            "phone"=>$dadr['phone'],
            "province"=>$dadr['province'],
            "city"=>$dadr['city'],
            "district"=>$dadr['district'],
            "address"=>$dadr['address'],
        );
        $d_json=json_encode($d,JSON_UNESCAPED_UNICODE);
        return urldecode($d_json);
    }
    //获取用户联系方式
    function getContactor($user_id=""){
        if($user_id==""){
            $user_id=session("user_id");
        }
        $Adr=M("Address");
        $dadr=$Adr->where(["user_id"=>$user_id,"is_default"=>1])->field("name,phone,province,city,district,address")->find();
        $d=array(
            "name"=>$dadr['name'],
            "phone"=>$dadr['phone']
        );
        $d_json=json_encode($d,JSON_UNESCAPED_UNICODE);
        return urldecode($d_json);
    }
    //获取用户姓名
    function getHeadImg($user_id=""){
        if($user_id==""){
            $user_id=session("user_id");
        }
        $d=$this->where(["user_id"=>$user_id])->field("headimgurl")->find();
        return $d['headimgurl'];
    }
    //获取用户卡号
    function getCard($user_id=""){
        if($user_id==""){
            $user_id=-1;
        }
        $d=M('card')->where(['user_id'=>$user_id])->find();
        return $d;
    }
    //获取当前用户积分
    function getScore($user_id=""){
        if($user_id==""){
            $user_id=-1;
        }
        $d=M('score_out')->where("user_id=$user_id")->field('sum(score) as score')->find();
        return $d['score'];
    }

}